#include "PlayGoalieBehaviour.h"

/*
 * Podla vyberu z if blokov sa brankar chova
 * funkcia je samovysvetlujuca, viac pisat nebudem
 */
void PlayGoalieBehaviour::Behave()
{
	/*cs_me_ballDistance = (world->me->pos - world->ball->pos).GetLength();

	G_TXTN("[%d] LPT_Main, state: %d",world->state->GetSimTime(),world->state->GetPlayMode());
	G_TXTN("  LPT_CatchBall me:(%.2f,%.2f), v:(%.2f,%.2f)",
		world->me->pos.GetX(),world->me->pos.GetY(),
		world->me->vel.GetX(),world->me->vel.GetY());
	G_TXTN("            ball:(%.2f,%.2f), v:(%.2f,%.2f) age: %d; valid: %d; len: %.2f",
		world->ball->pos.GetX(),world->ball->pos.GetY(),
		world->ball->vel.GetX(),world->ball->vel.GetY(),
		world->ball->GetAge(),world->ball->IsValid(),cs_me_ballDistance);*/


	if(!world->ball->IsValid() && world->ball->GetAge() >= 10)
	{
		//neviem, kde je lopta
		GetBallUnknownBehaviour()->Behave();
		return;
	}
	
	if(world->state->GetPlayMode() == PM_FreeKick_Our || world->state->GetPlayMode() == PM_GoalKick_Our)
	{
		//vykopavanie lopty (po zachyteni brankarom, alebo netrafeni sa supera na branu)
		GetKickOffBehaviour()->Behave();
		return;
	}
	
	DATA_STORAGE.goalieKickOff = GOALIE_GOFORBALL;	//nastavenie pre istotu
	
	if(GetCatchBehaviour()->TryCatchBall())
	{
		//chytanie lopty
		return;
	}
	//kopnutie do lopty!!!
	if(ShouldKick())
	{
		//kopnutie lopty
		return;
	}
	
	//lopta smeruje na branu alebo je nebezpecna
	if(DATA_STORAGE.BallHeadingOnOurGoal() || DATA_STORAGE.DangerousBall())
	{
		//LPT_BallHeadingOnGoal();
		GetBallHeadingOnGoalBehaviour()->Behave();
		return;
	}
	
	if(GetPassNearGoalBehaviour()->IsPassNearGoal())
	{
		if(GetPassNearGoalBehaviour()->Behave())
			return;
	}
	
	//upravenie pozicie hraca podla lopty a inych hodnot
	//obranny mod
	GetDefenseLineBehaviour()->Behave();
}

//mozme kopnut do lopty?
bool PlayGoalieBehaviour::ShouldKick()
{
	float kickable = world->params->server->ball_size + world->me->type->player_size + world->me->type->kickable_margin;
	if((world->me->pos - world->ball->pos).GetLength() < (0.8 * kickable))
	{
		//mozme kopnut 
		return true;
	}
	return false;
}
